
import {
	apiGetYwType,
	apiSearchYwType,
	apiSearchName,
	apiServerRegister,
	apiServerRegisterGetFabuArea,
	apiGetYuanData2Type,
	apiGetFTPData,
	apiPushDetail,
	apiSearchServer,
	apiGetServerList,
	apiDealForm,
	apiCollectFormPush
} from './serverResourceApi';

export const componentGetYwType = (ev,param) => {/*获取业务类型，网段区域，信息类型*/
 				apiGetYwType(param).then((res)=>{
 					if(res.data.code==='S100101'){
 						let searchList = res.data.data.list;
 						for(let i in searchList){
 							switch (searchList[i].type){
 								case 'business_type':
 								ev.treeSeach.business_type = searchList[i].childList;
 								break;
 								case 'info_type':
 								ev.treeSeach.info_type = searchList[i].childList;
 								break;
 								case 'segment':
 								ev.treeSeach.segment = searchList[i].childList;
 								break;
 								default:
 								break;
 							}
 						}
 						console.log(ev.formLabel);
 						ev.formLabel[ev.formLabel.length-1].options=ev.treeSeach.business_type;
 					}
 				})
};


export const componentSearchYwType = (ev,param) => {/*搜索业务类型，网段区域，信息类型*/
 	console.log(typeof(param.businessTypeIds));
 	apiSearchYwType({
 		'businessTypeIds':typeof(param.businessTypeIds)==='undefined'?null:[param.businessTypeIds],
 		'serviceType':1,
 		'name':param.name||''
 	}).then((res)=>{
 		console.log(res);
 		let searchList = res.data.data.list;
 		ev.tableData = [];
 		for (let i in searchList){
 			ev.tableData.push({
 				id:searchList[i].id,
 				resocureName:searchList[i].name,
 				serverName:searchList[i].producer.name,
 				serverType:searchList[i].businessTypeName, 
 				createrData:searchList[i].createTime,
 			})
 		}
 	})
};

export const componentSearchName = (ev,param) => {/*获取服务方名称*/
			 apiSearchName(param).then((res)=>{
			 	console.log(res);
			 	if(res.data.code==='S100101'){
			 		ev.fuwuName = res.data.data.list;
			 	}
			 })

};

export const componentServerRegisterGetFabuArea=(ev,param)=>{/*获取资源网段的发布区域*/
	apiServerRegisterGetFabuArea({"segment":param}).then((res)=>{
		console.log(res);
		if(res.data.code==="S100101"){
			ev.fabuAreaRadio = res.data.data.list;
		}
		
	})
}

export const componentServerRegister = (ev,param) => {/*注册提交*/
			console.log(param);
			if(Array.isArray(param.businessType)){
				param.businessType = param.businessType.join();
			}
			if(Array.isArray(param.infoType)){
				param.infoType = param.infoType.join();
			}
			if(Array.isArray(param.nodeIds)){
				param.nodeIds = param.nodeIds.join();
			}
			
			apiServerRegister(param).then((res)=>{
			 	console.log(res);
			 	if(res.data.code==="S100101"){
			 		ev.$message({
				            type: 'success',
				            message: res.data.msg
				     });  
			 	}else{
				 	ev.$message({
				            type: 'info',
				            message: res.data.msg
				     });  
			 	}
			})
};


export const componentGetYuanData2Type = (ev,param,typdId) => {/*获取元数据，共享返回类的*/
			apiGetYuanData2Type(param).then((res)=>{
			 	console.log(res);
			 	if(res.data.code==="S100101"){
			 		let yuanDataList = res.data.data.list
			 		for(let i in yuanDataList){
			 			if(yuanDataList[i].type === typdId){
			 				ev.wenjianFormYuanDataOptions.push(yuanDataList[i]);
			 			}
			 		}
			 	}
			})
};

export const componentGetFTPData = (ev,typdId,resourceWeb) => {/*ftp数据源*/
			apiGetFTPData({typeId:typdId,"segmentId":resourceWeb}).then((res)=>{
			 	console.log(res);
			 	if(res.data.code==="S100101"){
			 		ev.FTPData = res.data.data.list;
			 	}
			})
};

export const componentPushDetail = (ev,param) => {/*获取发布服务详情*/
			apiPushDetail(param).then((res)=>{
			 	console.log(res);
			 	let arr = ["","接口","文件","数据库"];
			 	if(res.data.code==="S100101"){
			 		ev.searchResult[0].value=res.data.data.id;
			 		ev.searchResult[1].value=res.data.data.producer.name;
			 		ev.searchResult[2].value=res.data.data.producer.contacts.split(';')[0].split(',')[0];
			 		ev.searchResult[3].value=res.data.data.producer.contacts.split(';')[0].split(',')[1];
			 		ev.searchResult[4].value=arr[res.data.data.inServiceDataSource.type];
			 		ev.searchResult[5].value=arr[res.data.data.describe];
			 		/*************************文件表单还是接口表单*******/ 
			 		ev.outputData=res.data.data.inServiceDataSource.outputDataFormList
			 		console.log(ev.outputData);
			 		for (let i in res.data.data.outServiceDataSourceList){
			 			if(res.data.data.outServiceDataSourceList[i].type===1){/*返回接口*/
			 				ev.showForm.shishiJiekou.show=true;
			 				ev.showForm.shishiJiekou.id=res.data.data.outServiceDataSourceList[i].id;
			 			}
			 			if(res.data.data.outServiceDataSourceList[i].type===2){/*返回文件*/
			 				ev.showForm.wenjian.show=true;
			 				ev.showForm.wenjian.id =res.data.data.outServiceDataSourceList[i].id;
			 				ev.filePath = res.data.data.outServiceDataSourceList[i].filePath;
			 			}
			 		}
			 		if(res.data.data.accessTactics){
			 			ev.generalAccessPolicy[0].value = res.data.data.accessTactics.frequency+ev.frequencyArray[ res.data.data.accessTactics.frequencyUnit-1]||""
			 			ev.generalAccessPolicy[1].value = res.data.data.accessTactics.flow+ev.flowArray[res.data.data.accessTactics.flowUnit-1]||""
			 			ev.generalAccessPolicy[2].value = res.data.data.accessTactics.timeValue===null?'不限':ev.timeTpyeArray[res.data.data.accessTactics.timeTpye-1]+':'+res.data.data.accessTactics.timeValue;
			 		}
			 		
			 		/*****************************************/
			 		ev.areaPush = res.data.data.areaList;
			 	}
			})
};

export const componentSearchServer = (ev,businessTypeIds,infoTypeIds) => {/*获取选择的资源*/
			apiSearchServer({
				"businessTypeIds":[businessTypeIds],
				"serviceType":1,
				"infoTypeIds":[infoTypeIds]
			}).then((res)=>{
			 	console.log(res);
			 	if(res.data.code==="S100101"){
			 		ev.searchType=res.data.data.list;
			 	}
			})
};


export const componentDealForm = (ev) => {/*计算即将发布的数据*/
		ev.form.nodeIds = ev.serverPushform.areaPush.join();

		console.log(ev.form);
		ev.tongyongTimeSubmit=!ev.tongyongTimeSubmit;
//		ev.wenjianTimeSubmit =!ev.wenjianTimeSubmit;	
//		ev.shishiTimeSubmit =!ev.shishiTimeSubmit;
		
		console.log(ev.form);
		console.log(ev.serverPushform);
};

export const componentServerPublic =(ev)=>{/*提交发布的数据*/
		apiDealForm(ev.form).then((res)=>{
				if(res.data.code==="S100101"){
			 		ev.$message({
				            type: 'success',
				            message: res.data.msg
				     });  
			 	}else{
				 	ev.$message({
				            type: 'info',
				            message: res.data.msg
				     });  
			 	}
		})
}

export const componentGetServerList = (ev) => {/*获取服务方*/
			apiGetServerList({
				"name":"",
				"type":0,
			}).then((res)=>{
			 	console.log(res);
			 	if(res.data.code==="S100101"){
			 		ev.everyAreaCheckBox=res.data.data.list;
			 	}
			})
};


export const componentCollectFormPush =(ev)=>{/*提交采集服务的数据*/
		apiCollectFormPush(ev.form).then((res)=>{
				if(res.data.code==="S100101"){
			 		ev.$message({
				            type: 'success',
				            message: res.data.msg
				     });  
			 	}else{
				 	ev.$message({
				            type: 'info',
				            message: res.data.msg
				     });  
			 	}
		})
}

